package com.realgodo.site.web;

/**
 * 
 */

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;
import org.hibernate.SessionFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.Preparable;
import com.realgodo.site.service.DomainNameAware;

/**
 * Struts2中典型REST CRUD Action的抽象基类. 实现ModelDriven<T>,Preparable接口
 * 
 * @param <T>
 *            CRUDAction所管理的对象类型.
 * 
 * @author luke
 * 
 */
@SuppressWarnings("serial")
public abstract class RESTControllerSupport<T> extends ActionSupport implements
		ModelDriven<T>, Preparable, DomainNameAware {
	protected Logger logger = LoggerFactory.getLogger(getClass());
	protected Long id;
	protected SessionFactory sessionFactory;
	protected HttpServletRequest request;
	protected String domainUserName;

	/**
	 * Action函数，默认action函数 GET = /users
	 * 
	 * @return
	 */
	public String index() throws Exception {
		return "index";
	}

	/**
	 * Action函数 GET = /users/1/
	 * 
	 * @return
	 */
	public String show() throws Exception {
		return "show";
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public void prepare1() throws Exception {
		// TODO Auto-generated method stub
		return;
	}

	@Autowired
	public void setSessionFactory(final SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

	public HttpServletRequest getRequest() {
		return request;
	}

	public void setRequest(HttpServletRequest request) {
		this.request = request;
	}

	/**
	 * Convenience method to get the response
	 * 
	 * @return current response
	 */
	protected HttpServletResponse getResponse() {
		return ServletActionContext.getResponse();
	}

	/**
	 * Convenience method to get the session. This will create a session if one
	 * doesn't exist.
	 * 
	 * @return the session from the request (request.getSession()).
	 */
	protected HttpSession getSession() {
		return getRequest().getSession();
	}

	@Override
	public void setDomainUserName(String domainUserName) {
		this.domainUserName=domainUserName;
	}

	public String getDomainUserName() {
		return domainUserName;
	}
	
}
